package com.atguigu.filter;

import com.atguigu.pojo.User;
import com.atguigu.pojo.UserManager;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class ManagerFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest)servletRequest;

        User user = (User)httpServletRequest.getSession().getAttribute("user");
        UserManager userManager = (UserManager)httpServletRequest.getSession().getAttribute("userManager");

        Object user_manager = httpServletRequest.getParameter("user_manager");

        if(userManager != null){
            filterChain.doFilter(servletRequest, servletResponse);
        } else if (user_manager.equals("1")){
            servletRequest.getRequestDispatcher("/pages/user/login_manager.jsp").forward(servletRequest, servletResponse);
        }
        // 如果等于null说明没有登入
        else if (user == null) {
            servletRequest.getRequestDispatcher("/pages/user/login.jsp").forward(servletRequest, servletResponse);
        } else {
            // 放行
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
